< previous page page_206 next page >

Page 206
If Not bTaxable Then
    cSalesTax = 0
    cUseTax = 0
End If
Handling True and False Conditions
The other way of handling False conditions allows you to process different sets of instructions for the True or False condition. You can handle this "fork in the road" in Visual Basic with the Else part of the If statement block.
To handle both the True and False conditions, you start with the block If statement and add the Else statement, as follows:
If condition Then
   statements to process when condition is True
Else
   statements to process when condition is False
End If
The If and End If statements of this block are the same as before. The condition is still any logical expression or variable that yields a True or False value. The key element of this set of statements is the Else statement. This statement is placed after the last statement to be executed if the condition is True, and before the first statement to be executed if the condition is False. For a True condition, the program processes the statements up to the Else statement and then skips to the first statement after the End If. If the condition is False, the program skips the statements prior to the Else statement and starts processing with the first statement after the Else.
note.gif If you want to execute code for only the False portion of the statement, you can just place code statements between the Else and End If statements. You are not required to place any statements between the If and Else statements. square.gif

tip.gif
If you have several commands between the If and End If statements, you might want to repeat the condition as a comment in the End If statement, as in this example:
If cTotalSales > cProjectedSales Then
'
' A bunch of lines of code
'
Else
'
' Another bunch of lines of code
'
End If 'cTotalSales > cProjectedSales
Adding this comment makes your code easier to read.

Working with Multiple If Statements
In the preceding sections, you saw the simple block If statements, which evaluate one condition and can execute commands for either a True or a False condition. You can also evaluate multiple conditions with an additional statement in the block If. The ElseIf statement allows

 
< previous page page_206 next page >